package com.laborder.sysyy.controller.courseTable;


import com.laborder.sysyy.entity.*;
import com.laborder.sysyy.entity.Dictionary;
import com.laborder.sysyy.service.course.CourseService;
import com.laborder.sysyy.service.coursetable.CourseTableService;
import com.laborder.sysyy.service.dictionary.DictionaryService;
import com.laborder.sysyy.service.labOrder.LabOrderService;
import com.laborder.sysyy.utils.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.*;


@Controller
@RequestMapping(value = "courseTable")
public class CourseTableController {
	@Autowired
	private LabOrderService labOrderService;
	@Autowired
	private DictionaryService dictionaryService;
	@Autowired
	private CourseTableService courseTableService;
	@Autowired
	private CourseService courseService;
	/**
	 * @Function: 返回实验室预约页面数据接口
	 * @author: 786107529@qq.com FJL
	 * @date:2019/1/5 17:45
	 */
	@GetMapping("tocourseTable")
	public String tocourseTable(Model model,String weekNum){

		int noCourse = 0;
		if(weekNum==null){
			weekNum = "1";
		}
		List<Dictionary> dictionaryList = dictionaryService.findDictionary();
		List<Dictionary> weekNameList = new ArrayList<Dictionary>();
		List<CourseTable> courseTableList = courseTableService.findCourse(weekNum);
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("week")){
				weekNameList.add(d);
			}
		}
		model.addAttribute("weekNameList",weekNameList);
		if(courseTableList.size()!=0){
			model.addAttribute("courseTableList",courseTableList);
		}else{
			model.addAttribute("noCourse",noCourse);
		}
		String thisYear = DateUtils.getYear();
		String  nextYear = String.valueOf(Integer.parseInt(thisYear)+1);
		model.addAttribute("weekNum",weekNum);
		model.addAttribute("thisYear",thisYear);
		model.addAttribute("nextYear",nextYear);
		return "/courseTable/courserTableList";
	}
	@GetMapping("tocourseTableAdd")
	public String tocourseTableAdd(Model model){
		List<Course> courseList = courseService.findAllCourse();
		List<Dictionary> dictionaryList = dictionaryService.findDictionary();
		List<Dictionary> weekNameList = new ArrayList<Dictionary>();
		List<Dictionary> weekendNameList = new ArrayList<Dictionary>();
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("week")){
				weekNameList.add(d);
			}
		}
		for(Dictionary d:dictionaryList){
			if(d.getDictionaryCode().equals("weekend")){
				weekendNameList.add(d);
			}
		}
		model.addAttribute("weekNameList",weekNameList);
		model.addAttribute("weekendNameList",weekendNameList);
		model.addAttribute("courseList",courseList);
		return "/courseTable/courseTableAdd";
	}

	@ResponseBody
	@RequestMapping("saveCourseTable")
	public  int saveCourseTable(CourseTable courseTable,String weekNum,String weekNum1,String weekend,String time) {
		int flag = 0;
		int num = 0;
		int num1 = 0;
		if(weekend.equals("1")){
			courseTable.setMonday("1");
		}else if(weekend.equals("2")){
			courseTable.setTuesday("1");
		}else if(weekend.equals("3")){
			courseTable.setWednesday("1");
		}else if(weekend.equals("4")){
			courseTable.setThursday("1");
		}else if(weekend.equals("5")){
			courseTable.setFriday("1");
		}else if(weekend.equals("6")){
			courseTable.setSaturday("1");
		}else if(weekend.equals("7")){
			courseTable.setSunday("1");
		}
		if(time.equals("12")){
			courseTable.setOneToTwo("1");
		}else if(time.equals("34")){
			courseTable.setThreetoFour("1");
		}else if(time.equals("56")){
			courseTable.setFivetoSix("1");
		}else if(time.equals("78")){
			courseTable.setSenventoEight("1");
		}else if(time.equals("910")){
			courseTable.setNinetoTen("1");
		}else if(time.equals("1112")){
			courseTable.setEleventoTwelve("1");
		}

		try{
			 num = Integer.parseInt(weekNum);
			 num1 = Integer.parseInt(weekNum1);
		}catch (NumberFormatException e){
			e.printStackTrace();
		}
		int week = num1-num;
		for(int i=0;i<=week;i++){

			String Stringweek=String.valueOf((num+i));
			courseTable.setWeekNum(Stringweek);
			courseTable.setId(UUID.randomUUID().toString());
			flag = courseTableService.saveCourseTable(courseTable);
		}
		
		return flag;
	}
	@ResponseBody
	@RequestMapping("delLabOrderInfo")
	public boolean delLabOrderInfo(String loId) {
		boolean flag = false;
		int num=0;
		num = labOrderService.delLabOrderInfo(loId);
		if(num==0){
			 flag = false;
		}else{
			flag=true;

		}
		return flag;
	}
	


	
}